package com.holdtime.jtt809.taxi.module.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.holdtime.jtt809.taxi.module.entity.PlatformInfoDO;
import com.holdtime.jtt809.taxi.module.entity.mapper.PlatformInfoMapper;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformInfoPageReqVO;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformInfoSaveReqVO;
import com.holdtime.jtt809.taxi.module.service.PlatformInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.holdtime.jtt809.taxi.module.enums.Taxi809ErrorCodeConstants.PLATFORM_INFO_NOT_EXISTS;

/**
 * 下级平台信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class PlatformInfoServiceImpl implements PlatformInfoService {

    @Resource
    private PlatformInfoMapper platformInfoMapper;

    @Override
    public String createPlatformInfo(PlatformInfoSaveReqVO createReqVO) {
        // 插入
        PlatformInfoDO platformInfo = BeanUtils.toBean(createReqVO, PlatformInfoDO.class);
        platformInfoMapper.insert(platformInfo);
        // 返回
        return platformInfo.getId();
    }

    @Override
    public void updatePlatformInfo(PlatformInfoSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformInfoExists(updateReqVO.getId());
        // 更新
        PlatformInfoDO updateObj = BeanUtils.toBean(updateReqVO, PlatformInfoDO.class);
        platformInfoMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformInfo(String id) {
        // 校验存在
        validatePlatformInfoExists(id);
        // 删除
        platformInfoMapper.deleteById(id);
    }

    private void validatePlatformInfoExists(String id) {
        if (platformInfoMapper.selectById(id) == null) {
            throw exception(PLATFORM_INFO_NOT_EXISTS);
        }
    }

    @Override
    public PlatformInfoDO getPlatformInfo(String id) {
        return platformInfoMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformInfoDO> getPlatformInfoPage(PlatformInfoPageReqVO pageReqVO) {
        return platformInfoMapper.selectPage(pageReqVO);
    }

}